//
//  MainTabBar.swift
//  bboySinaWeibo
//
//  Created by apple on 15/11/11.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUPUI()
    }
    
    //如果重写了 init(frame:CGRect) 这个方法  必须实现这个方法 系统会认为通过代码创建的控件
    required init?(coder aDecoder: NSCoder) {
        //如果走storyboard 或者 xib 就会出现下面的错误  
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUPUI () {
        
        //添加撰写按钮
        addSubview(editBtn)
    }
    
    //布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义递增变量
        var index :CGFloat = 0
        for subView in subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRectOffset(rect, w * index, 0)
                
                if index == 1 {
                    
                    index++
                }
                index++
            }
            
            //设置撰写按钮的位置
            editBtn.frame = CGRectOffset(rect, w * 2, 0)
            bringSubviewToFront(editBtn)

        }
        
        //遍历子控件 把中间的按钮位置确定
        
    }
    //懒加载撰写按钮
    lazy var editBtn : UIButton = {
        
        let btn = UIButton()
        
        //设置撰写安妮的图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
            
        btn.sizeToFit()
        return btn
    }()
}
